<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <title>Isolated execution patern</title>
  <xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css">
</head>
<body id="bodyID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
          <tbody>
    <tr id="hdr">
            <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
            </td>
            <td class="product">
            </td>
          </tr>
  </tbody>
</table>
      </div>
<div id="TitleRow">
<h1 class="dtH1" align="left">"Isolated execution" pattern</h1>
      </div>
    </div>
<div id="nstext">
<p>
"Isolated execution" pattern represents the simplest execution
scenario. According to this pattern a host application and a script
have no knowledge about each other. In fact the script execution is not
very different from execution from command prompt the except, that the
execution is initiated by the application but not by the user.</p>
<h4 class="dtH4">Example</h4>
<p>Let's assume we have script <i>SendMail.cs</i>, which that contains
implementation of sending an e-mail. And it uses command-line arguments
to obtain the e-mail address, subject and e-mail text. If it is
required to send an email from the command prompt this command would
look like this:</p>
<div class="syntax">cscs SendMail&nbsp;smith@gmail.com Test "This is just a test"</div>
<p>&nbsp;</p>
<p><i>
Script execution within current process:</i></p>
<p>
Any script can be executed from the host application within a current
process. This is because the script engine is available not only as an
executable but also as a class library (<a href="Installation.html">CSScriptLibrary.dll</a>).</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;CSScriptLibrary;<br>
      <br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;SendMail<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>&nbsp;[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">string</font>[]&nbsp;params&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;<font color="#0000ff" size="2">string</font>[]&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<font color="#800000" size="2">"SendMail"</font>,&nbsp;<font color="#800000" size="2">"smith@gmail.com"</font>,&nbsp;<font color="#800000" size="2">"Test"</font>,&nbsp;<font color="#800000" size="2">"This&nbsp;is&nbsp;just&nbsp;a&nbsp;test"</font>};<br>
      <br>
      <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSScript.Execute(null,&nbsp;params);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<p>
&nbsp;</p>
<p>
<i>
Script execution as a separate process:</i></p>
<p>
Any script can be also executed from the host application as a separate
process. In this case the script code for sending the e-mail would be
as simple as:<br>
</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2">RunScript(<font color="#800000" size="2">"SendMail&nbsp;smith@gmail.com&nbsp;Test&nbsp;\"This&nbsp;is&nbsp;just&nbsp;a&nbsp;test\""</font>);</font></td>
    </tr>
  </tbody>
</table>
<p>
The following C# code is an implementation of the RunScript method that
executes the script file and waits until execution is completed.</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;RunScript(<font color="#0000ff" size="2">string</font>&nbsp;scriptFileCmd)<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Process&nbsp;proc&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;Process();&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.StartInfo.FileName&nbsp;=&nbsp;<font color="#800000" size="2">"cscs.exe"</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.StartInfo.Arguments&nbsp;=&nbsp;<font color="#800000" size="2">"/nl"</font>&nbsp;&nbsp;+&nbsp;scriptFileCmd;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.StartInfo.UseShellExecute&nbsp;=&nbsp;<font color="#0000ff" size="2">false</font>;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.StartInfo.RedirectStandardOutput&nbsp;=&nbsp;<font color="#0000ff" size="2">true</font>;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.StartInfo.CreateNoWindow&nbsp;=&nbsp;<font color="#0000ff" size="2">true</font>;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.Start();<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">string</font>&nbsp;line&nbsp;=&nbsp;null;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">while</font>&nbsp;(null&nbsp;!=&nbsp;(line=&nbsp;proc.StandardOutput.ReadLine()))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(line);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc.WaitForExit();<br>
}</font></td>
    </tr>
  </tbody>
</table>
<p></p>
<p>
This method is implemented in C#, however the similar implementation can be done in almost any programming language.</p>
<p>
&nbsp;</p>
<h4 class="dtH4">
See Also</h4>
<p>
<a href="ProgrammingReference.html">Reference</a> | <a href="Installation.html"> Installation</a>&nbsp;</p>
<p>
&nbsp;</p>
<p>
&nbsp;</p>
<object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e" viewastext="true" style="display: none;">
	<param name="Keyword" value="Script Hosting">
</object> 
    </div>
</body>
</html>

